﻿using GDI_应用案例.Pages.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_应用案例.Pages
{
    [FormPage(7, "曼德勃罗集")]
    public partial class PageMandelbrot : PageBase
    {
        readonly Mandelbrot _mandelbrot = new Mandelbrot();
        public PageMandelbrot()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if(!checkBox2.Checked && !checkBox1.Checked)
            {
                MessageBox.Show("至少选择一个");
                return;
            }

            button1.Enabled = false;

            pictureBox1.Image?.Dispose();
            _mandelbrot.MaxIterations = (int)this.numericUpDown1.Value;
            _mandelbrot.RenderSpot = GetRenderSpot(this.comboBox1.SelectedIndex);
            if (checkBox1.Checked)
                _mandelbrot.PolynomialRules = new PolynomialRules();
            else
                _mandelbrot.PolynomialRules = null;

            if (checkBox2.Checked)
                _mandelbrot.OtherRules = new OtherRules();
            else
                _mandelbrot.OtherRules = null;

            int n = (int)numericUpDown2.Value;
            string txt = comboBox2.Text;
            pictureBox1.Image = await Task.Run(() =>
            {
                return _mandelbrot.DrawMandel(n, txt);
            });

            button1.Enabled = true;
        }
        private int _lastIndex=-1;
        private IRenderSpot _last;
        private IRenderSpot GetRenderSpot(int index)
        {
            IRenderSpot renderSpot = default;
            if (_lastIndex == index)
                renderSpot = _last;
            else
            {
                if (index == 0)
                    renderSpot = new RenderSpotTradition();
                else if (index == 1)
                    renderSpot = new RenderSpotHSV();
                else if (index == 2)
                    renderSpot = new RenderSpotLog();
                else if (index == 3)
                    renderSpot = new RenderSpotSmooth();
                else if (index == 4)
                    renderSpot = new RenderSpotPredefine();
                else
                    renderSpot = new RenderSpotGray();

                _lastIndex = index;
                _last = renderSpot;
            }

            return renderSpot;
        }

       
    }
}
